//
// Unihan Input Method
//
// Copyright © 2002-2007 Sun Wah Linux Ltd.
//
//
// This library is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of the
// License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
// USA
//
// $Id: punc_keyboard.c 757 2007-05-10 08:19:31Z roger $
//

/*
** $Id: punc_keyboard.c 757 2007-05-10 08:19:31Z roger $
**
*/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif /* HAVE_CONFIG_H */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>

#include <gtk/gtk.h>
#include <gdk/gdkx.h>

#include <gconf/gconf-client.h>

#include <X11/X.h>
#include <X11/Xatom.h>
#include <X11/Xproto.h>

#include "xaux_common.h"
#include "xaux_ext_common.h"

#include "unihan_xaux.h"
#include "punc_keyboard.h"
#include "key_punc_images.h"
#include "punc.h"
#include "global.h"

#include "unihan-gconf.h"

static GtkWidget *keyboard_window = NULL;

static KeyboardKey punc_keys[] = {
	/* 1st row */
	{ &UNIHAN_punc_key_wave,	0,	0,	'`' },
	{ &UNIHAN_punc_key_1,		22,	0,	'1' },
	{ &UNIHAN_punc_key_2,		44,	0,	'2' },
	{ &UNIHAN_punc_key_3,		66,	0,	'3' },
	{ &UNIHAN_punc_key_4,		88,	0,	'4' },
	{ &UNIHAN_punc_key_5,		110,	0,	'5' },
	{ &UNIHAN_punc_key_6,		132,	0,	'6' },
	{ &UNIHAN_punc_key_7,		154,	0,	'7' },
	{ &UNIHAN_punc_key_8,		176,	0,	'8' },
	{ &UNIHAN_punc_key_9,		198,	0,	'9' },
	{ &UNIHAN_punc_key_0,		220,	0,	'0' },
	{ &UNIHAN_punc_key_minus,	242,	0,	'-' },
	{ &UNIHAN_punc_key_equal,	264,	0,	'=' },
	{ &UNIHAN_punc_key_backslash,	286,	0,	'\\' },
	{ &UNIHAN_punc_key_backspace,	308,	0,	'\b' },
	/* 2nd row */
	{ &UNIHAN_punc_key_tab,		0,	24,	'\t' },
	{ &UNIHAN_punc_key_q,		33,	24,	'q' },
	{ &UNIHAN_punc_key_w,		55,	24,	'w' },
	{ &UNIHAN_punc_key_e,		77,	24,	'e' },
	{ &UNIHAN_punc_key_r,		99,	24,	'r' },
	{ &UNIHAN_punc_key_t,		121,	24,	't' },
	{ &UNIHAN_punc_key_y,		143,	24,	'y' },
	{ &UNIHAN_punc_key_u,		165,	24,	'u' },
	{ &UNIHAN_punc_key_i,		187,	24,	'i' },
	{ &UNIHAN_punc_key_o,		209,	24,	'o' },
	{ &UNIHAN_punc_key_p,		231,	24,	'p' },
	{ &UNIHAN_punc_key_openbracket,	253,	24,	'[' },
	{ &UNIHAN_punc_key_closebracket,275,	24,	']' },
	{ &UNIHAN_punc_key_enter1,	297,	25,	'\n' },
	/* 3rd row */
	{ &UNIHAN_punc_key_caps,	0,	48,	'\x14' },
	{ &UNIHAN_punc_key_a,		44,	48,	'a' },
	{ &UNIHAN_punc_key_s,		66,	48,	's' },
	{ &UNIHAN_punc_key_d,		88,	48,	'd' },
	{ &UNIHAN_punc_key_f,		110,	48,	'f' },
	{ &UNIHAN_punc_key_g,		132,	48,	'g' },
	{ &UNIHAN_punc_key_h,		154,	48,	'h' },
	{ &UNIHAN_punc_key_j,		176,	48,	'j' },
	{ &UNIHAN_punc_key_k,		198,	48,	'k' },
	{ &UNIHAN_punc_key_l,		220,	48,	'l' },
	{ &UNIHAN_punc_key_semicolon,	242,	48,	';' },
	{ &UNIHAN_punc_key_quotes,	264,	48,	'\'' },
	{ &UNIHAN_punc_key_enter2,	286,	48,	'\n' },
	{ &UNIHAN_punc_key_enter3,	297,	48,	'\n' },	
	/* 4th row */
	{ &UNIHAN_punc_key_left_shift,	0,	72,	'\0' },
	{ &UNIHAN_punc_key_z,		55,	72,	'z' },
	{ &UNIHAN_punc_key_x,		77,	72,	'x' },
	{ &UNIHAN_punc_key_c,		99,	72,	'c' },
	{ &UNIHAN_punc_key_v,		121,	72,	'v' },
	{ &UNIHAN_punc_key_b,		143,	72,	'b' },
	{ &UNIHAN_punc_key_n,		165,	72,	'n' },
	{ &UNIHAN_punc_key_m,		187,	72,	'm' },
	{ &UNIHAN_punc_key_comma,	209,	72,	',' },
	{ &UNIHAN_punc_key_period,	231,	72,	'.' },
	{ &UNIHAN_punc_key_slash,	253,	72,	'/' },
	{ &UNIHAN_punc_key_right_shift,	275,72,	'\0' },
	/* 5th row */
	{ &UNIHAN_punc_key_left_ctrl,	0,	96,	'\0' },
	{ &UNIHAN_punc_key_esc,		33,	96,	'\x1b' },
	{ &UNIHAN_punc_key_left_alt,	66,	96,	'\0' },
	{ &UNIHAN_punc_key_space,	99,	96,	' ' },
	{ &UNIHAN_punc_key_right_alt,	231,	96,	'\0' },
	{ &UNIHAN_punc_key_right_ctrl,	297,	96,	'\0' },

	{ NULL,				0,	0,	'\0'}
};


static gboolean punc_key_clicked(GtkWidget*, GdkEventButton*, char*);

//void send_to_engine(int, int *, int, char **);


gboolean
move_keyboard_window(GtkWidget *w, GdkEventButton *event, char* ch)
{
	GtkWidget* ancestor;
	ancestor = gtk_widget_get_toplevel(w);
	printf(">>> move_keyboard_window\n");
	gtk_window_begin_move_drag( GTK_WINDOW(ancestor), event->button,
	                            event->x_root, event->y_root,
	                            event->time );
	printf("<<< move_keyboard_window\n");
	return TRUE;
}

static gboolean
punc_key_clicked(GtkWidget *w, GdkEventButton *e, char* ch)
{
	int int_values[1] = { UNIHAN_ENGINE_COMMIT };
	char *str_values[1];

	/* we only process the first mouse button, so we leave if this
	 * event is not produced by the first button */
	if ( e->button != 1 )
	{
		return FALSE;
	}

	str_values[0] = find_punctuation((int)*ch);

	if ( str_values[0] != NULL )
	{
		/* commit the punctuation */
		send_to_engine(1, int_values, 1, str_values);

	    //    int_values[0] = UNIHAN_ENGINE_TOGGLE_PUNC;
		//send_to_engine(1, int_values, 0, NULL);

		//hide_punc_keyboard();
	}
	return TRUE;
}


void
keyboard_window_expose(GtkWidget *w, GdkEventExpose* e, gpointer d)
{
	gdk_draw_rectangle( GTK_LAYOUT(w)->bin_window,
			    w->style->black_gc, FALSE, 0, 0,
			    KEYBOARD_FULL_WIDTH-1, KEYBOARD_FULL_HEIGHT-1);
	gdk_draw_rectangle( GTK_LAYOUT(w)->bin_window,
			    w->style->black_gc, FALSE,
	                    2, 2, KEYBOARD_WIDTH+1, KEYBOARD_HEIGHT+1);
/*
	gdk_draw_rectangle( GTK_LAYOUT(w)->bin_window,
			    w->style->black_gc,
	                    TRUE, KEYBOARD_BORDER, KEYBOARD_BORDER,
	                    KEYBOARD_WIDTH-KEYBOARD_BORDER,
	                    KEYBOARD_HEIGHT-KEYBOARD_BORDER );
*/
}

static void
draw_punc_keyboard(GtkWidget* key_images[], int NO_OF_KEYS)
{
	GtkWidget*	keyboard_layout;
	GtkWidget*	event_boxes[NO_OF_KEYS];
	int		i;

	keyboard_layout = gtk_layout_new(NULL, NULL);
	keyboard_window = gtk_window_new(GTK_WINDOW_POPUP);

	gtk_container_add(GTK_CONTAINER(keyboard_window), keyboard_layout);

	for ( i = 0; i < NO_OF_KEYS; i++ )
	{
		GdkPixbuf* tmp_pixbuf;

		tmp_pixbuf = gdk_pixbuf_from_pixdata( punc_keys[i].image,
						      FALSE, NULL );
		key_images[i] = gtk_image_new_from_pixbuf(tmp_pixbuf);
		event_boxes[i] = gtk_event_box_new();

		gtk_container_add(GTK_CONTAINER(event_boxes[i]), key_images[i]);

		gtk_layout_put( (GtkLayout*)keyboard_layout,
				event_boxes[i],
				punc_keys[i].x + KEYBOARD_BORDER,
				punc_keys[i].y + KEYBOARD_BORDER);
		gtk_widget_set_events(key_images[i], GDK_BUTTON_PRESS_MASK);
		//g_signal_connect( G_OBJECT(event_boxes[i]),
		//		  "button_release_event",
		//		  G_CALLBACK(punc_key_clicked),
		//		  &(punc_keys[i].ch) );
		g_signal_connect( G_OBJECT(event_boxes[i]),
				  "button_press_event",
				  G_CALLBACK(punc_key_clicked),
				  &(punc_keys[i].ch) );
		g_signal_connect( G_OBJECT(keyboard_layout),
		                  "expose_event",
		                  G_CALLBACK(keyboard_window_expose), NULL);
	}

	g_signal_connect( G_OBJECT(keyboard_layout),
			  "button_press_event",
			  G_CALLBACK(move_keyboard_window),
			  NULL );
}


void
show_punc_keyboard(void)
{
	int		screen_w, screen_h;
	int		i;
	static Bool	cursor_already_set = FALSE;
	static int	NO_OF_KEYS = (sizeof punc_keys) / (sizeof (KeyboardKey)) - 1;
	GtkWidget*	key_images[NO_OF_KEYS];

printf("showing punctuation keyboard: %d\n", __LINE__);
	if (!keyboard_window) {
		draw_punc_keyboard(key_images, NO_OF_KEYS);
	}

	screen_w = gdk_screen_width();
	screen_h = gdk_screen_height();
	gtk_window_set_gravity(GTK_WINDOW(keyboard_window), GDK_GRAVITY_SOUTH_EAST);
	// FIXME: sizes are wrong!
//	gtk_window_move(GTK_WINDOW(keyboard_window), screen_w - 328, screen_h - panel_applet_get_size() - 127);
	gtk_window_set_default_size(GTK_WINDOW(keyboard_window),
	                            KEYBOARD_FULL_WIDTH,
	                            KEYBOARD_FULL_HEIGHT);
	gtk_window_move(GTK_WINDOW(keyboard_window),
                        screen_w - 20 - KEYBOARD_FULL_WIDTH,
 	                screen_h - 20 - KEYBOARD_FULL_HEIGHT);
	gtk_widget_show_all(keyboard_window);
	gtk_window_present(GTK_WINDOW(keyboard_window));

	/* to set the cursors... need to do it after widgets have been
	 * realized... */
	if ( ! cursor_already_set )
	{
		for ( i = 0; i < NO_OF_KEYS; i++ )
		{
			gdk_window_set_cursor( key_images[i]->window,
					       gdk_cursor_new(GDK_CENTER_PTR) );
		}
		cursor_already_set = TRUE;
	}
}

void
hide_punc_keyboard(void)
{
	if ( keyboard_window ) {
		printf("hiding punctuation keyboard: %d\n", __LINE__);
		//gtk_widget_destroy(keyboard_window);
		//keyboard_window = NULL;
		gtk_widget_hide(keyboard_window);
	}
}

// vi:ts=4:nowrap:cin:expandtab
